#include "TableModel.h"



//-------------------------------------

TableModel::TableModel( int r, int c, QObject * pobj )
    : QAbstractTableModel( pobj ),
      rCnt( r ),
      cCnt( c )
{
}

//-------------------------------------

QVariant TableModel::data( const QModelIndex & index, int nRole ) const
{
    if( !index.isValid() )
    {
        return QVariant();
    }
    QString str;// = QString( "%1,%2" ).arg( index.row()+1 ).arg( index.column()+1 );
    str.clear();
    return ( nRole == Qt::DisplayRole || nRole == Qt::EditRole ) ? modelData.value( index, QVariant( str ) ) : QVariant();
}

//-------------------------------------

bool TableModel::setData( const QModelIndex & index, const QVariant & value, int nRole )
{
    if( index.isValid() && nRole == Qt::EditRole )
    {
        modelData[ index ] = value;
        emit dataChanged( index, index );
        return true;
    }
    return false;
}

//-------------------------------------

int TableModel::rowCount( const QModelIndex & ind ) const
{
    return rCnt;
}

//-------------------------------------

int TableModel::columnCount( const QModelIndex & ind ) const
{
    return cCnt;
}

//-------------------------------------

Qt::ItemFlags TableModel::flags( const QModelIndex & index ) const
{
    Qt::ItemFlags flags = QAbstractTableModel::flags( index );
    return index.isValid() ? ( flags | Qt::ItemIsEditable ) : flags;
}

//-------------------------------------

void TableModel::ClearData()
{
    for( int i=0; i <  rCnt; i++ )
        removeRow( i );
    rCnt = 0;
    modelData.clear();
}

//-------------------------------------
